<?php

defined( '_JEXEC' ) or die( 'Restricted access' );
require_once( JApplicationHelper::getPath( 'admin_html' ));
require_once( JApplicationHelper::getPath( 'toolbar_html' ));
JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
switch($task)
{
	case 'edit' :
	case 'add' :
		editTransport( $option );
		break;
	
	case 'apply' :
	case 'save' :
		saveTransport( $option, $task );
		break;
	
	case 'remove' :
		removeTransport( $option );
		break;
	
	default :
		showTransport( $option );
		break;
}

function editTransport( $option )
{
	$row =& JTable::getInstance('transport', 'Table');
	$cid = JRequest::getVar( 'cid', array(0), '', 'array');
	$id = $cid[0];
	$row->load($id);	
	$lists = array();
	$lists['status'] = JHTML::_('select.booleanlist', 'status','class="inputbox"', $row->status);	
	HTML_transports::editTransports($row, $lists, $option);
}

function saveTransport($option, $task)
{
	// buat upload
	// Configuration - Your Options
	$allowed_filetypes = array('.kml'); 
	$max_filesize = 524288; 
	$upload_path = '/bee/PETA/jalur/';
 
	$filename = $_FILES['userfile']['name'];
	$path = $upload_path.''.$filename;
	$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
 
	if(!in_array($ext,$allowed_filetypes))
		die('The file you attempted to upload is not allowed.');
 
	if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
		die('The file you attempted to upload is too large.');
 
	if(!is_writable($upload_path))
		die('You cannot upload to the specified directory, please CHMOD it to 777.');
 
	if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
		echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>';
	else
		echo 'There was an error during the file upload.  Please try again.'; 

	global $mainframe;
	$row =& JTable::getInstance('transport', 'Table');
	if(!$row->bind(JRequest::get('post')))
	{
		echo "<script>alert('".$row->getError()."'); 
		window.history.go(-1);</script>\n";
		exit();
	}
	
	$row->path_file = $path;
	$row->nama_file = $filename;
	
	if(!$row->store())
	{
		echo "<script>alert('".$row->getError()."'); 
		window.history.go(-1);</script>\n";
		exit();
	}
	
	 switch ($task)
	{
		case 'apply':
			$msg = 'Changes to Review saved';
			$link = 'index.php?option=' . $option . '&task=edit&cid[]='. $row->id;
			break;
		case 'save':
		default:
			$msg = 'Review Saved';
			$link = 'index.php?option=' . $option;
		break;
	}
	$mainframe->redirect($link, $msg);
}

function showTransport( $option )
{
	$db =& JFactory::getDBO();
	$query = 'select * from jos_transport';
	$db->setQuery($query);
	$rows = $db->loadObjectList();
	if($db->getErrorNum())
	{
		echo $db->stderr();
		return false;
	}
	HTML_transports::showTransports($option, $rows);
}

function removeTransport( $option )
{
  global $mainframe;
  $cid = JRequest::getVar( 'cid', array(), '', 'array' );
  $db =& JFactory::getDBO();
  if(count($cid))
  {
    $cids = implode( ',', $cid );
    $query = "DELETE FROM jos_transport WHERE id IN ( $cids )";
    $db->setQuery( $query );
    if (!$db->query()) 
    {
      echo "<script> alert('".$db->getErrorMsg()."'); 
      window.history.go(-1); </script>\n";
    }
  }
  $mainframe->redirect( 'index.php?option=' . $option );
}

?>